#include "NetCal.hpp"
#include "Protocol.hpp"
#include "TcpServer.hpp"
#include <memory>

void Usage(std::string proc)
{
    std::cerr << "Usage: " << proc << " port" << std::endl;
}

// ./tcpserver port
int main(int argc ,char *argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        exit(USAGE_ERR);
    }
    //1. 业务层
    std::unique_ptr<Cal> cal = std::make_unique<Cal>();

    //2. 协议层
    std::unique_ptr<Protocal> protocol = std::make_unique<Protocal>([&cal](Request &req)->Response{
        return cal->Excute(req);
    });

    //3. 服务器层 
    std::unique_ptr<TcpServer> tsvr = std::make_unique<TcpServer>(std::stoi(argv[1]),
    [&protocol](std::shared_ptr<Socket> &sock,InetAddr &client){
        protocol->GetRequest(sock,client);
    }
    );
    tsvr->Start();

    // 基类指针指向派生类,实现多态特性
    // std::unique_ptr<Socket> tcpsockp = std::unique_ptr<TcpSocket>();
    // tcpsockp->BuildListenSocket();
    // std::unique_ptr<Socket> udpsockp = std::unique_ptr<UdpSocket>();
    // udpsockp->BuildListenSocket();
    return 0;
}